/**
 * @(#) CongMoneyMediator.as
 *
 * @date		2009. 02. 17.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	경조사 신청 - Mediator
 * 				경조사을 조회하고 신청하는 Presentation Logic을 관리
 */
package com.skt.common_hr.congmoney2.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.congmoney2.CongMoney2Facade;
	import com.skt.common_hr.congmoney2.events.CongGwarekeyEvent;
	import com.skt.common_hr.congmoney2.model.CelebrationProxy;
	import com.skt.common_hr.congmoney2.model.CongMoney2ListProxy;
	import com.skt.common_hr.congmoney2.model.CongMoney2Proxy;
	import com.skt.common_hr.congmoney2.model.vo.CongMoneyWriteVO;
	import com.skt.common_hr.congmoney2.view.components.CelebrationDetailView;
	import com.skt.common_hr.congmoney2.view.components.CongMoney2OverView;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class CongMoney2Mediator extends OverViewMediator
	{
		public static const NAME:String = "CongMoney2Mediator";


		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function CongMoney2Mediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
			setEventListener();
 			setInitialData();
			super.initializeView();
 		}

		override protected function activatedView():void
		{
			super.activatedView();
 			view.vsCompensation.selectedIndex = 0;
 			loadCongMoenyList();
		}

		override protected function closedView():void
		{
			removeEventListener();
		}


		private function setInitialData():void
		{
			view.BEGDA = DateUtil.firstDateOfMonth(new Date());
			view.ENDDA = DateUtil.todayNoTime();
		}
 		private function setEventListener():void
 		{
 			view.addEventListener(CongMoney2OverView.SELECT_LIST, onClickSelectList);
 			view.addEventListener(CongGwarekeyEvent.SELECT_DATA, onClickReadOnly);
 			view.addEventListener(CongGwarekeyEvent.UPDATE_DATA, onClickModify);
 			
 			view.addEventListener(CongGwarekeyEvent.PRINT_DATA, onClickPrint);
 			
 			//20110916
 			view.addEventListener(CongGwarekeyEvent.PRINT_DATA2, onClickPrint2);

			view.congDetailView.addEventListener(CelebrationDetailView.ClosedDetailView, onCloseDetailView); 			
 		}

		private function onInputCancel(event:Event):void
		{
			clearView();
			//facade.sendNotification(BizTripFacade.CHANGE_VIEW_BIZTRIP_LIST, this);
			bActivated = false;
		}
		
 		private function removeEventListener():void
 		{
			view.removeEventListener(CongGwarekeyEvent.SELECT_DATA, onClickReadOnly);
 		}

		private function onClickSelectList(e:Event):void
		{
			loadCongMoenyList();
		}

 		private function loadCongMoenyList():void
 		{
 			var proxy:CongMoney2ListProxy = facade.retrieveProxy(CongMoney2ListProxy.NAME) as CongMoney2ListProxy;
 			proxy.loadList(Variables.myPernr, view.BEGDA, view.ENDDA, view.selectedStatus);
 		}




		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return basicInterestMessages.concat([
					CongMoney2ListProxy.LOAD_CONGMONEY_LIST_COMPLETE,
					CongMoney2ListProxy.LOAD_CONGMONEY_LIST_FAILED,
					CongMoney2Proxy.LOAD_CELEBRATION_DETAIL_COMPLETE,
					CongMoney2Proxy.LOAD_CELEBRATION_DETAIL_FAILED,
					ReqCelebration2Mediator.CLOSED_REQUEST_VIEW,
					CongMoney2Facade.LOAD_CONGMONEY_PRINT_COMPLETE,
					CongMoney2Facade.LOAD_CONGMONEY_CORSNVIEW_COMPLETE
					]);
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName())
			{
				case CongMoney2ListProxy.LOAD_CONGMONEY_LIST_COMPLETE:
					//Alert.show("성공");
					view.congMoneyDataList = (note.getBody() as CongMoney2ListProxy).congMoneyList;
					break;
				case CongMoney2ListProxy.LOAD_CONGMONEY_LIST_FAILED:
					//Alert.show("실패");
					break;
				case CongMoney2Proxy.LOAD_CELEBRATION_DETAIL_COMPLETE:
					setDetailView((note.getBody() as CongMoney2Proxy).detailVO);
					break;
				case CongMoney2Proxy.LOAD_CELEBRATION_DETAIL_FAILED:
					break;
					
				case ReqCelebration2Mediator.CLOSED_REQUEST_VIEW:
					view.vsCompensation.selectedIndex = 0;
					loadCongMoenyList();
					break;
					
				case CongMoney2Facade.LOAD_CONGMONEY_PRINT_COMPLETE:
					onPrintDataLoad(note.getBody() as CelebrationProxy);
					break;

					
				case CongMoney2Facade.LOAD_CONGMONEY_CORSNVIEW_COMPLETE:
					onCorsnLoad(note.getBody() as CelebrationProxy);
					break;											
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}

 		
 		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function setListView(list:ArrayCollection):void
 		{
 			view.congMoneyDataList = list;
 			commonFacade.setStatusMessage("STATUS_LOADED_LIST", [list.length.toString()]);
 			if(list.length == 0) {
 				//view.showInitialTip();
 			}
 		}


		/**
		 * list조회가 성공했을 때 호출됨
		 *     - 조회상세보기
		 * @param
		 * @return void
		 */
 		private function setDetailView(vo:CongMoneyWriteVO):void
 		{
 			//var proxy:SupportStandardProxy = facade.retrieveProxy(SupportStandardProxy.NAME) as SupportStandardProxy;
 			//view.congDetailView.funeral = proxy.isFuneral(vo.CORSN);
 			view.congDetailView.dataProvider = vo;
 		}


 		//사유(프린트)
 		private function onCorsnLoad(proxy:CelebrationProxy):void
 		{
 			var index:int;
 			
 			if(Variables.company == "SAC") {
	 			index = findCodeIndex(view.printViewSac.vo.CORSN,proxy.congCorsn);
	 			
	 			if(index != -1) {
	 				view.printViewSac.cornm = proxy.congCorsn[index].label;
	 			}
	
	 			view.printViewSac.doPrint();
	 		}
	 		else {
	 			index = findCodeIndex(view.printView.vo.CORSN,proxy.congCorsn);
	 			
	 			if(index != -1) {
	 				view.printView.cornm = proxy.congCorsn[index].label;
	 			}
	
	 			view.printView.doPrint();
	 		}
 		}
		
		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - 프린트
		 * @param
		 * @return void
		 */
		
 		private function onPrintDataLoad(proxy:CelebrationProxy):void
 		{
 			//PopUpManager.addPopUp(view.printView, view);
 			//PopUpManager.centerPopUp(view.printView);
 			if(Variables.company == "SAC") {
 				view.printViewSac.vo = proxy.printDataVO;
 			}
 			else {
 				view.printView.vo = proxy.printDataVO;
 			}
 			//view.printView.doPrint();
 			
 			//사유
 			var corsGubun:String;
 			if(proxy.printDataVO.CPERNR == Variables.myPernr) {
				corsGubun = ""; //본인인경우
			} else {
				corsGubun = "X"; //대상자병경시
			}
			var corsnProxy:CelebrationProxy = facade.retrieveProxy(CelebrationProxy.NAME) as CelebrationProxy;
			corsnProxy.corsnLoad(corsGubun);
 		}










		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////

		/**
		 * 경조사 조회버튼 이벤트
		 * @param
		 * @return void
		 */
 		private function onClickReadOnly(event:CongGwarekeyEvent):void
 		{
 			commonFacade.setStatusMessage("");
 			view.vsCompensation.selectedIndex = 2;

			var congProxy:CongMoney2Proxy = facade.retrieveProxy(CongMoney2Proxy.NAME) as CongMoney2Proxy;
			congProxy.loadDetail(event.key);

 		}

 		/**
		 * 경조사   신규 신청 버튼 처리
		 * @param
		 * @return void
		 */
 		private function onClickNew(event:Event):void
 		{

 			var param:Object = new Object();
 			param.pernr 	= Variables.myPernr;
 			param.name  	= Variables.myName;
 			param.gwarekey  = "";

 			executeReqView(param);
 		}


 		/**
		 * 수정 버튼 클릭시 실행
		 * @param
		 * @return void
		 */
 		private function onClickModify(event:CongGwarekeyEvent):void
 		{
			var param:Object = new Object();
 			param.gwarekey  = event.key;
 			param.pernr = event.cpernr;
 			param.name  = Variables.myName;

			//Alert.show('CongMoney2Mediator.as@onClickModify');//silver
			
 			executeReqView(param);
 		}


 		/**
		 * 프린트
		 * @param
		 * @return void
		 */
 		private function onClickPrint(event:CongGwarekeyEvent):void
 		{
 			var congProxy:CelebrationProxy = facade.retrieveProxy(CelebrationProxy.NAME) as CelebrationProxy;
			congProxy.printData(event.key);
 		}
		
		// 20110916 결재창 view를 띄움 . 
 		private function onClickPrint2(event:CongGwarekeyEvent):void
 		{
 			//var congProxy:CelebrationProxy = facade.retrieveProxy(CelebrationProxy.NAME) as CelebrationProxy;
			//congProxy.printData(event.key);
			
			var medi:ApprovalViewMediator = facade.retrieveMediator(ApprovalViewMediator.NAME) as ApprovalViewMediator;
			if(medi==null) {
				medi = new ApprovalViewMediator();
				facade.registerMediator(medi);
			}			
			medi.execView(view, event.key);			
 		}
 		


 		/**
		 * 경조사 조회닫기
		 * @param
		 * @return void
		 */
 		private function onCloseDetailView(event:Event):void
 		{
 			view.vsCompensation.selectedIndex = 0;
 			//setStatusMessage("");
 		}



 		/**
		 * 경조사 신규 신청 및 수정 화면 실행
		 * @param
		 * @return void
		 */
 		private function executeReqView(param:Object):void
 		{
 			//Alert.show('executeReqView');//silver 
 			
 			commonFacade.setStatusMessage("");
			view.vsCompensation.selectedIndex = 1;
			 
 			var writeMediator:ReqCelebration2Mediator = facade.retrieveMediator(ReqCelebration2Mediator.NAME) as ReqCelebration2Mediator;
 			
 			if(!writeMediator) {
				writeMediator = new ReqCelebration2Mediator(view.congWriteView);
	 			facade.registerMediator(writeMediator);
			}
			
 			if(writeMediator) facade.sendNotification(CongMoney2Facade.LOAD_CONGMONEY_WRITE_INITIAL, param); 
 			
 						
 		}




		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		private function findCodeIndex(key:String, srcArr:ArrayCollection):int
		{
			for(var i:int=0; i<srcArr.length; i++) {
				if(srcArr[i].data == key) {
					return i;
				}
			}
			return -1;
		}

       private function get view():CongMoney2OverView
        {
            return viewComponent as CongMoney2OverView;
        }


	}
}